home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniPlayer / Mini Movie Stuff.c < prev    next >
C/C++ Source or Header  |  1991-09-05  |  9KB  |  367 lines

  1. /**************************************************
  2. *
  3. * Moov stuff
  4. *
  5. *
  6. ***************************************************/
  7.  
  8. #include "Movies.h"
  9. #include "Mini Player.h"
  10.  
  11.  
  12. /**************************************************
  13. *
  14. * Globals
  15. *
  16. ***************************************************/
  17.  
  18. /*    Info about the movie */
  19. Movie    theMovie;
  20. Rect    dispBounds;
  21.  
  22. OSErr        theErr;                    /* Saves having to declare it everywhere */
  23.  
  24. /* Stuff for cursor control */
  25. ControlHandle movieScrollControlH; 
  26. Boolean        trackingHand;
  27. Point        trackStart;
  28. Fixed        playSpeed;
  29.  
  30. extern    WindowPtr    moovWindow;        /* Window picture is displayed in */
  31. extern    Cursor        playCursor, reverseCursor, pauseCursor, blinkCursor;
  32.  
  33.  
  34. /**************************************************
  35. *
  36. * SetUpMovies() Initializes the movie tools
  37. *
  38. ***************************************************/
  39. void SetUpMovies()
  40.  
  41. {
  42.     theErr = EnterMovies();        /* This would normally with the other manager inits */
  43.     if (theErr) DebugStr((StringPtr)"\pEnterMovies Failed");
  44.  
  45.     theMovie = 0;                /* Set theMovie to 0 to mark it as unused */
  46.     
  47. }
  48.  
  49.  
  50.  
  51. /**************************************************
  52. *
  53. * OpenTheMovie(fn,vRef) 
  54. *    Opens the movie named fn and starts it up
  55. *    on exit:
  56. *        moovWindow = resized and titled window
  57. *        m = movie
  58. *
  59. ***************************************************/
  60. void OpenTheMovie(fn,vRef)
  61. Str255    fn;
  62. int        vRef;
  63. {
  64.     short     movieResRefNum;
  65.     FSSpec        mySpec;                    /* File System record for the file */
  66.  
  67.     
  68.     /* Make an FSSpec record of the info */
  69.     theErr = FSMakeFSSpec(vRef,0,fn,&mySpec);
  70.     
  71.     /* First open the movie file */
  72.     if (theErr = OpenMovieFile(&mySpec, &movieResRefNum, 0))
  73.         return;                                    /* Bail out if it didn't work */
  74.  
  75.     if (theErr = NewMovieFromFile( &theMovie,movieResRefNum, nil, nil,0, nil ))
  76.         return;                                    /* Bail out if it didn't work */
  77.  
  78.     CloseMovieFile(movieResRefNum);                /* We're done with the resource fork */
  79.  
  80.     if (theErr = GetMoviesError())                /* Bail out if it didn't work */
  81.         return;
  82.  
  83.     /* Get the bounds for the movie  and make sure the top left is 0,0 */
  84.     /* so the movie won't be offset within the window */
  85.     GetMovieBox( theMovie, &dispBounds);
  86.     OffsetRect(&dispBounds,-dispBounds.left,-dispBounds.top);
  87.     SetMovieBox(theMovie, &dispBounds);        
  88.  
  89.     if (theErr = GetMoviesError())                /* Bail out if it didn't work */
  90.         return;
  91.     
  92.  
  93.     /* Set up the window for the movie to play in */
  94.     SizeWindow(moovWindow,dispBounds.right,dispBounds.bottom + 16,false);
  95.     SetWTitle(moovWindow,fn);                /* Set the title. You could use the movie name */
  96.     ShowWindow(moovWindow);                    /* Make it visible */
  97.     SelectWindow(moovWindow);                /* and in front */
  98.  
  99.     /* Put a scroll control along the bottom */
  100.     MakeMovieControls();
  101.     
  102.     SetMovieGWorld(theMovie,nil,nil);        /* Play the movie in the window */
  103.     GotoBeginningOfMovie(theMovie);
  104.     PrerollMovie(theMovie,0,0);                /* Get the movie ready to play */
  105.     SetMovieActive(theMovie,true);
  106.     StartMovie(theMovie);                    /* Start the movie */
  107.  
  108.  
  109.     /* Initialize stuff for cursor control */
  110.     trackingHand = false;
  111.     playSpeed = x1Speed;
  112.  
  113. }
  114.  
  115. /**************************************************
  116. *
  117. * MakeMovieControls() Puts the controls at the bottom of the window
  118. *
  119. ***************************************************/
  120. void MakeMovieControls()
  121.  
  122. {
  123.     Rect hScrollRect;
  124.     int    min, max, curVal;
  125.     
  126.     min = 0;
  127.     max = GetMovieDuration(theMovie);        /* What if the movie is over 4 min? */
  128.     curVal = 0;
  129.     
  130.     hScrollRect = moovWindow->portRect;        /* Size the scroll bar */
  131.     hScrollRect.bottom += 1;
  132.     hScrollRect.right += 1;
  133.     hScrollRect.left -= 1 ;
  134.     hScrollRect.top = hScrollRect.bottom - 17;
  135.     
  136.     movieScrollControlH = NewControl(moovWindow,&hScrollRect,(StringPtr)"\p",true,
  137.                                     curVal,min,max,scrollBarProc,0L);     
  138.  
  139. }
  140.  
  141. /**************************************************
  142. *
  143. * MyMoviesTask() Calls MoviesTask and stops at the end
  144. *    This is called from the main event loop of the application
  145. *
  146. ***************************************************/
  147. void MyMoviesTask()
  148.  
  149. {    
  150.     if (theMovie)
  151.     {
  152.         SetCtlValue(movieScrollControlH,GetMovieTime(theMovie,nil));    /* Update thumb to current time */
  153.         
  154.         MoviesTask(theMovie,0);
  155.         if (IsMovieDone(theMovie))            /* Has movie reached the end */
  156.         {    
  157.             StopMovie(theMovie);            /* Make sure it stopped */
  158.             playSpeed = 0;                    /* Switch to the Pause cursor */
  159.         }
  160.     }
  161. }
  162.  
  163.  
  164. /**************************************************
  165. *
  166. * CleanUpMovie() Throws out the movie
  167. *
  168. ***************************************************/
  169. void CleanUpMovie()
  170.  
  171. {
  172.     if(theMovie)
  173.     {
  174.         DisposeMovie(theMovie);
  175.         DisposeControl(movieScrollControlH);
  176.         theMovie = 0;
  177.     }
  178. }
  179.  
  180.  
  181. /**************************************************
  182. *
  183. * MovieScrollProc(theControl, theCode) 
  184. *    Procedure called from TrackControl when tracking the scroll bar
  185. *    The buttons move the movie by 1 movie time unit
  186. *    The page up/down move the movie by a second
  187.  
  188. *
  189. ***************************************************/
  190. pascal void MovieScrollProc(ControlHandle theControl, int theCode)
  191. {
  192.     int    curControlValue,minControlValue,maxControlValue,theDelta;
  193.     
  194.     curControlValue = GetCtlValue(theControl);    /* Get scroll value & limits */
  195.     minControlValue = GetCtlMin(theControl);
  196.     maxControlValue = GetCtlMax(theControl);
  197.     
  198.     switch (theCode)
  199.     {
  200.         case    inPageDown:
  201.             theDelta = GetMovieTimeScale(theMovie);    /* The timescale is 1 secs worth */
  202.             break;
  203.         
  204.         case    inDownButton:
  205.             theDelta = +1;
  206.             break;
  207.                             
  208.         case    inPageUp:
  209.             theDelta = -GetMovieTimeScale(theMovie);
  210.             break;
  211.         
  212.         case    inUpButton:
  213.             theDelta = -1;
  214.             break;
  215.     }
  216.     
  217.     curControlValue += theDelta;                /* Get the new value */
  218.     if (curControlValue < minControlValue)        /* Make sure it is in range */
  219.         curControlValue = minControlValue;
  220.     else if (curControlValue > maxControlValue)
  221.         curControlValue = maxControlValue;
  222.     
  223.     SetMovieTimeValue(theMovie,curControlValue);    /* Position the movie to the new time */
  224.     MyMoviesTask();                                    /* Call our movies task routine
  225.                                                     to update the control and the movie */
  226.  
  227. }
  228.  
  229. /**************************************************
  230. *
  231. * MovieMouseDown(theWindow, thePoint) 
  232. *    Mouse pressed in movie content
  233. *
  234. ***************************************************/
  235. void MovieMouseDown(theWindow, thePoint)
  236.  
  237. WindowPtr    theWindow;
  238. Point        thePoint;
  239.  
  240. {
  241.     ControlHandle    theControl;
  242.     short            thePart;
  243.     
  244.     if( theWindow == moovWindow)
  245.     {
  246.         GlobalToLocal(&thePoint);    /* Convert to window's local coords */
  247.         thePart = FindControl(thePoint,theWindow,&theControl);    /* In a control? */        
  248.         
  249.         if (!thePart)
  250.         {
  251.             /* In the content area */
  252.             trackingHand = true;        /* Flag we are tracking the movie */
  253.             trackStart = thePoint;        /* Save where it was pressed */
  254.         }
  255.         else
  256.         {
  257.             /* In the scroll control */
  258.                 if (thePart == inThumb)
  259.                 {
  260.                 
  261.                     thePart = TrackControl(theControl,thePoint,nil);
  262.                     SetMovieTimeValue(theMovie,GetCtlValue(movieScrollControlH));
  263.                 }
  264.                 else
  265.                     thePart = TrackControl(theControl,thePoint,(ProcPtr) &MovieScrollProc);
  266.                     
  267.         }
  268.     }
  269. }
  270.  
  271. /**************************************************
  272. *
  273. * MovieMouseUp(theWindow, thePoint)
  274. *    Mouse released in movie content
  275. *    If mouse moved right > 8, add one to playing speed.
  276. *    If mouse moved left > 8, subtract one from playing speed
  277. *    If mouse moved <= 8 stop
  278. *
  279. ***************************************************/
  280. void MovieMouseUp(theWindow, thePoint)
  281.  
  282. WindowPtr    theWindow;
  283. Point        thePoint;
  284.  
  285. {
  286.     int        amountMoved;
  287.     
  288.     if( trackingHand && (theWindow == moovWindow))
  289.     {
  290.         /* Figure out new playing speed */
  291.         GlobalToLocal(&thePoint);    /* Convert to window's local coords */
  292.         amountMoved = thePoint.h - trackStart.h;
  293.         
  294.         if (amountMoved < -8)                /* Moved left? */
  295.         {                                    /* If so, make speed more negative */
  296.             playSpeed -=x1Speed;            /* It's a fixed point number!!!!! */
  297.             if (playSpeed == 0)                /* Skip over 0 */
  298.                 playSpeed = -x1Speed;
  299.         }
  300.         else if (amountMoved > 8)            /* Moved right? */
  301.         {
  302.             playSpeed +=x1Speed;
  303.             if (playSpeed == 0)                /* Skip over 0 */
  304.                 playSpeed = x1Speed;
  305.         }
  306.         else                                /* Didn't move so pause */
  307.             playSpeed = 0;
  308.             
  309.         SetMovieRate(theMovie,playSpeed );    /* Set the new rate befire calling IsMovieDone */                    
  310.  
  311.         /* If playing forward and at end, go back to the start */
  312.         if( (playSpeed > 0) && (theErr = IsMovieDone(theMovie)) )
  313.             GotoBeginningOfMovie(theMovie);
  314.  
  315.         /* Or if playing backwards and at start, go back to the end */
  316.         else if( (playSpeed < 0) && (theErr = IsMovieDone(theMovie)) )
  317.             GotoEndOfMovie(theMovie);
  318.         
  319.     }
  320. }
  321.  
  322.  
  323. /**************************************************
  324. *
  325. * MovieCursor()
  326. *
  327. *    Sets to proper movie cursor
  328. *    uses a different cursor for pause, forward, and reverse play
  329. *
  330. ***************************************************/
  331. void MovieCursor()
  332. {
  333.  
  334.     static    long    nextTime = 0;
  335.     static    Boolean inBlink = false;
  336.     long curTime;
  337.     
  338.     /* Blink the cursor every 20 ticks */
  339.     if ( (curTime = TickCount()) >= nextTime)
  340.     {
  341.         nextTime = curTime+20;
  342.         inBlink = !inBlink;
  343.     }
  344.     if (inBlink)
  345.         SetCursor(&blinkCursor);
  346.     else if (playSpeed > 0)
  347.         SetCursor(&playCursor);
  348.     else if (playSpeed < 0)
  349.         SetCursor(&reverseCursor);
  350.     else
  351.         SetCursor(&pauseCursor);
  352.  
  353. }
  354.     
  355. /**************************************************
  356. *
  357. * DoMovieUpdate()
  358. *
  359. *    Updates the movie screen
  360. *
  361. ***************************************************/
  362. void DoMovieUpdate()
  363. {
  364.     if (theMovie)
  365.         UpdateMovie(theMovie);
  366. }
  367.